import { $init, $digest } from '../../utils/common.util'
const predict_url="http://49.235.222.59:5000/predict"
var adds = {};
Page({
  

  data: {
    result:"we will return result for you!",
    images:"",
    tabs:[
      {
        id:0,
        name:"首页",
        isActive:true
      },
      {
        id:1,
        name:"说明",
        isActive:false
      },
      {
        id:2,
        name:"服务",
        isActive:false
      },
      {
        id:3,
        name:"团队",
        isActive:false
      },
    ]
  },  
  chooseImage(e) {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#00000",
      success: function (res) {
        console.log(res);
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  removeImage(e) {
    const idx = e.target.dataset.idx
    this.data.images.splice(idx, 1)
    $digest(this)
  },
  handleImagePreview(e) {
    const idx = e.target.dataset.idx
    const images = this.data.images
    console.log(images);
    wx.previewImage({
      
      current: images,  //当前预览的图片
      urls: images,  //所有要预览的图片
    })
  },  submit: function () {
    var that = this;
    that.upImgs(that.data.images[0])
  },
  chooseWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        that.data.images = res.tempFilePaths
        $digest(that)
      }
    }) 
  },

  upImgs: function (imgurl) {
    
    var that = this;
    console.log(imgurl);

    wx.uploadFile({
      url: 'http://49.235.222.59:5000/predict',//
      filePath: imgurl,
      name: 'file',
      header: {
        'content-type': 'multipart/form-data'
      },
      formData: null,
      success: function (res) {
        var response = JSON.parse(res.data)
        var result=response["result"]
        that.setData({
          result: result
        })
      },
      fail: function (res) {
        that.setData({
          result:"please add picture!"
        })
        console.log("error:"+res)

      }
    })
  },
  onLoad() {
    $init(this)
}
})